package com.hzya.frame.system.login.controller;

import com.hzya.frame.system.login.service.ILoginService;
import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.utils.core.action.DefaultController;
import com.hzya.frame.utils.core.action.SessionContext;
import com.hzya.frame.utils.core.entity.JsonResultEntity;
import com.hzya.frame.utils.help.CookieHelper;
import com.hzya.frame.utils.help.JsonHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author 郭军辉
 *
 *         2016年12月14日下午5:16:14
 */
@Controller(value = "login")
@RequestMapping(value = "/")
public class LoginController extends DefaultController {

	@Autowired
	private ILoginService loginService;

	/**
	 * 登录方法
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/dologin", method = { RequestMethod.POST })
	@ResponseBody
	public JsonResultEntity login(UserEntity user, HttpServletRequest req, HttpServletResponse response) {
		try {
			// 再判断密码
			if ("".equals(user.getLogin_name()) || "".equals(user.getPassword())) {
				return getFailureMessageEntity("用户名密码为空,请重新输入", "MMWK");
			} else {
				try {
					loginService.login(user);
					if ("".equals(CookieHelper.getAttribute("login_name", req))
							|| null == CookieHelper.getAttribute("login_name", req)) {
						CookieHelper.setAttribute("login_name", user.getLogin_name(), response);
					}
					if ("".equals(CookieHelper.getAttribute("platfrom_account", req))
							|| null == CookieHelper.getAttribute("platfrom_account", req)) {
					}
				} catch (Exception e) {
					e.getMessage();
					// 注意，此处用service获取到的抛出的异常的string即为json格式
					return JsonHelper.json2Bean(e.getMessage(), JsonResultEntity.class);
				}
				SessionContext.setCurrentUser(user);
				/** shiro认证 **/
				SecurityUtils.getSubject().login(new UsernamePasswordToken(user.getLogin_name(), user.getPassword()));
				return getSuccessMessageEntity("登录成功！", "TRUE");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return getFailureMessageEntity("系统错误，请联系管理员", "XTCW");
		}
	}

	/**
	 * 跳转登录页面
	 * @return
	 */
	@RequestMapping(value = "/login")
	public ModelAndView login() {
		ModelAndView view = new ModelAndView();
		view.setViewName("login");
		return view;
	}

	/**
	 * 跳转首页
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/index")
	public ModelAndView index() throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
		return modelAndView;
	}

	/***
	 * 退出登录，删除session里账户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/dologinout")
	public ModelAndView loginOut() {
		ModelAndView view = new ModelAndView();
		SessionContext.removeCurrentUser();
		SecurityUtils.getSubject().logout();
		view.setViewName("login");
		return view;
	}
}
